import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { DatePicker, Tooltip, Table, Form, Card, Button, Row, Col, Select } from 'antd';

import styles from '../List.less';
import { formatDate, getHtml } from '@/utils/utils';

import PageHeaderWrapper from '@/components/PageHeaderWrapper';

const FormItem = Form.Item;

@connect(({ log, loading }) => ({
  log,
  loading: loading.effects['log/fetchList'],
}))
@Form.create()
class List extends PureComponent {

  columns = [
    {
      title: '日志时间',
      dataIndex: 'createdAt',
      render: val => <span>{formatDate(val, 1, true)}</span>,
    },
    {
      title: '所用时间',
      dataIndex: 'costTime',
      render: val=><span>{val}ms</span>
    },
    {
      title: '级别',
      dataIndex: 'level',
    },
    {
      title: '请求信息',
      dataIndex: 'request',
      onCell: () => {
        return {
          style: {
            overflow: 'hidden',
            maxWidth: 300,
          }
        }
      },
      render: val=> 
        <Tooltip placement="left" title={<span dangerouslySetInnerHTML={{ __html: getHtml(val) }} />}>
          <div className={styles.ellipsis2} dangerouslySetInnerHTML={{ __html: getHtml(val) }} />
        </Tooltip>
    },
    // 状态码
    {
      title: '状态码',
      dataIndex: 'status',
    },
    // 返回信息
    {
      title: '返回信息',
      dataIndex: 'response',
      onCell: () => {
        return {
          style: {
            overflow: 'hidden',
            maxWidth: 300,
          }
        }
      },
      render: val=> 
        <Tooltip placement="left" title={<span dangerouslySetInnerHTML={{ __html: getHtml(val) }} />}>
          <div className={styles.ellipsis2} dangerouslySetInnerHTML={{ __html: getHtml(val) }} />
        </Tooltip>
    },
    // 错误信息
    {
      title: '错误信息',
      dataIndex: 'error',
      onCell: () => {
        return {
          style: {
            overflow: 'hidden',
            maxWidth: 300,
          }
        }
      },
      render: val=> 
        <Tooltip placement="left" title={<span dangerouslySetInnerHTML={{ __html: getHtml(val) }} />}>
          <div className={styles.ellipsis2} dangerouslySetInnerHTML={{ __html: getHtml(val) }} />
        </Tooltip>
    },
  ];

  constructor(props) {
    super(props);

    this.state = {
      // 搜索表单
      searchValues: {},
    };
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'log/fetchList',
      payLoad: {},
    });
  }

  // -------------- 查询列表 ------------------
  tableChange = (pagination, filters, sorter) => {
    const { dispatch } = this.props;
    const { searchValues } = this.state;

    const params = {
      pageNo: pagination.current,
      pageSize: pagination.pageSize,
    };

    dispatch({
      type: 'log/fetchList',
      payload: { ...params, ...searchValues },
    });
  }

  handleSearch = (e) => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) {
        return;
      }

      const values = {
        startDate: formatDate(fieldsValue.startDate, 2),
        endDate: formatDate(fieldsValue.endDate, 2),
      };

      this.setState({
        searchValues: values,
      });

      dispatch({
        type: 'log/fetchList',
        payload: values,
      });
    });
  }

  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      searchValues: {},
    });
    dispatch({
      type: 'log/fetchList',
      payload: {},
    });
  };

  // ------------ 搜索表单 ---------------
  renderSearch() {
    const {
      form: { getFieldDecorator },
    } = this.props;

    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
            <FormItem label="开始日期">
              {getFieldDecorator('startDate')(<DatePicker />)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <FormItem label="结束日期">
              {getFieldDecorator('endDate')(<DatePicker />)}
            </FormItem>
          </Col>
          <Col md={8} sm={24}>
            <span className={styles.submitButtons}>
              <Button type="primary" htmlType="submit">
                查询
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }

  render() {

    const {
      log: { rows, pagination },
      loading,
    } = this.props;

    return (
      <PageHeaderWrapper title='系统日志'>
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>
              {this.renderSearch()}
            </div>
            <div className={styles.standardTable}>
              <Table
                rowKey='_id'
                loading={loading}
                dataSource={rows}
                columns={this.columns}
                pagination={pagination}
                onChange={this.tableChange}
              />
            </div>
          </div>
        </Card>
      </PageHeaderWrapper>
    );
  }
}

export default List;
